using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using buildingLib.Prepare;

namespace _dmis.prepare
{
    public partial class FloorDlg : Form
    {
        Floors m_floors;
        public FloorDlg()
        {
            InitializeComponent();
        }

        private void FloorDlg_Load(object sender, EventArgs e)
        {
            projectListBindingSource.DataSource = ProjectList.GetProjects();
            
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            m_floors.AddFloor((Guid)this.projectListComboBox.SelectedValue, this.txt_Floor.Text, double.Parse(this.txt_Area.Text), this.txt_Memo.Text);

        }

        private void projectListComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.projectListComboBox.SelectedValue != null)
            {
                m_floors = Floors.GetFloors((Guid)this.projectListComboBox.SelectedValue);
                floorsBindingSource.DataSource = m_floors;
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            this.floorsBindingSource.RaiseListChangedEvents = false;
            this.floorsBindingSource.EndEdit();

            try
            {
                m_floors.Save();
            }
            catch (Csla.DataPortalException ex)
            {
                MessageBox.Show(ex.BusinessException.ToString(),
                  "Error saving", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                  "Error Saving", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
            }
            finally
            {
                this.floorsBindingSource.RaiseListChangedEvents = true;
                this.floorsBindingSource.ResetBindings(false);
            }
        }
      
    }
}